package red.social;

import android.app.Activity;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginVersion extends Activity implements OnClickListener {
	private String email;
	private EditText emailEditText;
	private Button acceder,b_english,b_spanish;

	private String ip;
	private String SERVLET_URL;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loginaux);

		ip = getResources().getString(R.string.ip);
		SERVLET_URL = "http://" + ip + ":8080/Modulo10-war/LoginVersionServlet";

		emailEditText = (EditText) this
				.findViewById(R.id.editTextCorreoVersion);
		acceder = (Button) this.findViewById(R.id.buttonEntrarVersion);
		b_english = (Button) findViewById(R.id.buttonEnglish);
		b_spanish = (Button) findViewById(R.id.buttonSpanish);

		// asignamos listeners
		acceder.setOnClickListener(this);
		b_english.setOnClickListener(this);
		b_spanish.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		String datos, sitio;

		if (v.equals(this.findViewById(R.id.buttonEntrarVersion))) {
			// recuperamos la informacion introducida
			this.email = this.emailEditText.getText().toString();

			Log.i("Boton Acceder", "Email: " + email);

			if (email.indexOf("@") == -1 || email == null || email.equals("")) { 
				Toast.makeText(this,"El email: " + email + " introducido no es valido",Toast.LENGTH_LONG).show();
			} else {
				// enviamos el email al Servlet para comprobar si es valido

				try {
					datos = "?parametro=ACCEDERATHIRTI&email=" + email;
					sitio = SERVLET_URL + datos;

					Log.i("Acceder", "Debo ir a: " + sitio);

					URL ob = new URL(sitio);
					URLConnection conn = ob.openConnection();
					conn.setDoInput(true);
					conn.setDoOutput(true);
					InputStream in = conn.getInputStream();
					DataInputStream dos = new DataInputStream(in);

					int vengoDe = dos.readInt();

					if (vengoDe == 1) {
						Toast.makeText(this,"El email introducido es incorrecto",Toast.LENGTH_LONG).show();
					} else if (vengoDe == 2) {

						Intent irAPassFace = new Intent();
						irAPassFace.setClass(this, PassFace.class);
						irAPassFace.putExtra("email", this.email);
						this.startActivity(irAPassFace);
					}
					
					// cerramos el flujo
					dos.close();
					
				} catch (IOException e) {
					Toast.makeText(this, "IOEx: " + e.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			}
		}else if(v.equals(this.findViewById(R.id.buttonEnglish))){
			String languageToLoad = "en";
			Locale locale = new Locale(languageToLoad);
			Locale.setDefault(locale);
			Configuration config = new Configuration();
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
			Intent i = new Intent(getApplicationContext(), LoginVersion.class);
			startActivity(i);
		}else if(v.equals(this.findViewById(R.id.buttonSpanish))){
			String languageToLoad = "es";
			Locale locale = new Locale(languageToLoad);
			Locale.setDefault(locale);
			Configuration config = new Configuration();
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
			Intent i = new Intent(getApplicationContext(), LoginVersion.class);
			startActivity(i);
		}
			
	}
}

